import { ProjectsApi } from '@/common/constants/request-api'
import { Method } from '@/common/enum/request-methods'
import type { ProjectCreateParam, ProjectDetail, Projects } from '@/common/types/projects'
import type { PagedParams, PagedResult } from '@/common/types/request'

/**
 * 随机获取一个项目
 */
export const getRandomProjectService = () => {
  return useRequest<Projects>({
    method: Method.GET,
    url: ProjectsApi.RANDOM_GET_PROJECT,
  })
}

/**
 * 分页获取项目
 * @param pagedParams 分页参数
 */
export const getPagedProjectsService = (pagedParams: PagedParams) => {
  return useRequest<PagedResult<Projects[]>>({
    method: Method.GET,
    url: ProjectsApi.PAGED_GET_PROJECTS,
    data: {
      ...pagedParams,
    },
  })
}

/**
 * 捐赠项目
 * @param projectId  项目id
 * @param amount  捐赠金额
 */
export const createProjectDonationService = (projectId: number, amount: number) => {
  const url = replaceParams(ProjectsApi.DONATIONS_PROJECT, [
    {
      paramName: 'id',
      value: projectId,
    },
  ])

  return useRequest({
    method: Method.POST,
    url,
    data: {
      amount,
    },
  })
}

/**
 * 根据项目id获取项目详情
 * @param projectId
 */
export const getProjectDetailService = (projectId: number) => {
  // 替换路径参数
  const url = replaceParams(ProjectsApi.GET_PROJECT_DETAIL, [
    {
      paramName: 'id',
      value: projectId,
    },
  ])

  return useRequest<ProjectDetail>({
    method: Method.GET,
    url,
  })
}

/**
 * 创建项目
 * @param data 项目信息
 */
export const createProjectService = (data: ProjectCreateParam) => {
  return useRequest({
    method: Method.POST,
    url: ProjectsApi.CREATE_PROJECT,
    data,
  })
}
